import Flutter
import UIKit
import StoreKit



public class SwiftMitScorePlugin: NSObject, FlutterPlugin {
    
  public static func register(with registrar: FlutterPluginRegistrar) {
    let channel = FlutterMethodChannel(name: "mit_score", binaryMessenger: registrar.messenger())
    let instance = SwiftMitScorePlugin()
    registrar.addMethodCallDelegate(instance, channel: channel)
  }

    public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        if #available(iOS 10.3, *) {
            SKStoreReviewController.requestReview()
        } else {
            // Fallback on earlier versions
            let arguments = call.arguments as! [String:Any]
            let appId = arguments["ios_id"] as! String
            let writeReview = arguments["write_review"] as! Bool
            var iTunesLink = ""
            if (writeReview) {
                iTunesLink = String(format: "https://apps.apple.com/cn/app/%@?action=write-review", appId)
            } else {
               
                iTunesLink = String(format: "https://apps.apple.com/cn/app/%@", appId)
            }
            let instagramURL = URL(string: iTunesLink)!

            if(UIApplication.shared.canOpenURL(instagramURL)) {
                UIApplication.shared.openURL(instagramURL);
            }
            result("iOS " + UIDevice.current.systemVersion)
        }
    }
}
